﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JobList
{
    public partial class FChangeJobList : Form
    {
        public Dictionary<string, string> FChangeJobListShowValue = new Dictionary<string, string>();
        IniFiles ini;
        static Singleton singleton = Singleton._instance;
        public FChangeJobList()
        {
            InitializeComponent();
        }

        private void FChangeJobList_Load(object sender, EventArgs e)
        {
            ini = new IniFiles(singleton.ConfigIniPath);
            switch (singleton.FChangeJobListShowValue)
            {
                case 0:
                    FChangeJobListShowValue.Add("FChangeJobList", ini.IniReadValue("Add", "FChangeJobList"));
                    FChangeJobListShowValue.Add("groupBox1", ini.IniReadValue("Add", "groupBox1"));
                    FChangeJobListShowValue.Add("BtnChange", ini.IniReadValue("Add", "BtnChange"));

                    this.Text = FChangeJobListShowValue["FChangeJobList"];
                    groupBox1.Text = FChangeJobListShowValue["groupBox1"];
                    BtnChange.Text = FChangeJobListShowValue["BtnChange"];
                    break;
                case 1:
                    FChangeJobListShowValue.Add("FChangeJobList", ini.IniReadValue("Modify", "FChangeJobList"));
                    FChangeJobListShowValue.Add("groupBox1", ini.IniReadValue("Modify", "groupBox1"));
                    FChangeJobListShowValue.Add("BtnChange", ini.IniReadValue("Modify", "BtnChange"));

                    this.Text = FChangeJobListShowValue["FChangeJobList"];
                    groupBox1.Text = FChangeJobListShowValue["groupBox1"];
                    BtnChange.Text = FChangeJobListShowValue["BtnChange"];

                    break;
            }

            #region 按钮控件方法
            this.BtnChange.Click += new System.EventHandler(this.BtnChange_Click);
            #endregion

            #region 修改加载数据
            LoadModifyData();
            #endregion
        }

        private void BtnChange_Click(object sender, EventArgs e)
        {
            CJobList cJobList = new CJobList();
            switch (singleton.FChangeJobListShowValue)
            {
                case 0:
                    try
                    {
                        cJobList = new CJobList
                        {
                            ID = Guid.NewGuid().ToString("N"),
                            CreateTime = DateTime.Now,
                            ModifyTime = DateTime.Now,
                            Proposer = TxtProposer.Text,
                            Executor = TxtExecutor.Text,
                            Title = TxtTitle.Text,
                            Content = "",
                            State = TxtState.Text,
                        };
                        string StrToDay = DateTime.Now.ToString("yyyyMMdd");
                        string ContentPath = singleton.JobListContentPath + @"\" + StrToDay;

                        cJobList.Content = ContentPath + @"\" + cJobList.ID + ".rtf";
                        singleton.IsExistDirPath(ContentPath);
                        singleton.cJobLists.Add(cJobList);
                        RTxtContent.SaveFile(cJobList.Content, RichTextBoxStreamType.RichText);
                        singleton.SavecJobLists();
                    }
                    catch
                    {
                        this.DialogResult = DialogResult.None;
                    }
                    this.DialogResult = DialogResult.OK;
                    break;
                case 1:
                    try
                    {
                        cJobList = singleton.cJobLists.Find((v) => v.ID == TxtID.Text);
                        cJobList.ModifyTime = DateTime.Now;
                        cJobList.Proposer = TxtProposer.Text;
                        cJobList.Executor = TxtExecutor.Text;
                        cJobList.Title = TxtTitle.Text;
                        cJobList.State = TxtState.Text;
                        if(string.IsNullOrEmpty(cJobList.Content))
                        {
                            string StrToDay = DateTime.Now.ToString("yyyyMMdd");
                            string ContentPath = singleton.JobListContentPath + @"\" + StrToDay;

                            cJobList.Content = ContentPath + @"\" + cJobList.ID + ".rtf";
                        }
                        RTxtContent.SaveFile(cJobList.Content, RichTextBoxStreamType.RichText);
                        singleton.SavecJobLists();
                    }
                    catch
                    {
                        this.DialogResult = DialogResult.None;
                    }
                    this.DialogResult = DialogResult.OK;
                    break;
            }           
        }

        private void LoadModifyData()
        {
            switch (singleton.FChangeJobListShowValue)
            {
                case 0:
                    TxtID.Enabled = false;
                    break;
                case 1:
                    TxtID.Enabled = false;
                    CJobList cJobList = singleton.cJobLists.Find((v) => v.ID == singleton.ActiveCJobListID);
                    TxtID.Text = cJobList.ID;
                    TxtProposer.Text = cJobList.Proposer;
                    TxtExecutor.Text = cJobList.Executor;
                    TxtTitle.Text = cJobList.Title;
                    TxtState.Text = cJobList.State;

                    if (!string.IsNullOrEmpty(cJobList.Content))
                    {
                        if(File.Exists(cJobList.Content))
                        {
                            RTxtContent.LoadFile(cJobList.Content, RichTextBoxStreamType.RichText);
                        }
                    }                    
                    break;
            }                   
        }
    }
}
